package com.cw.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.cw.data.ConstantEnum;
import com.cw.data.Result;
import com.cw.pojo.TbItemCat;
import com.cw.pojo.TbTypeTemplate;
import com.cw.service.ItemService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Arrays;
import java.util.List;

@Controller
@RequestMapping("itemController")
public class ItemController {

    @Reference
    private ItemService itemService;

    @RequestMapping("toItem")
    public String toItem(Model model) {

        List<TbTypeTemplate> tl = itemService.getTemplate();

        model.addAttribute("tl", tl);

        return "admin/item_cat";
    }

    @RequestMapping("getItemInfo")
    @ResponseBody
    public Result getItemInfo(Long id) {

        try {

            List<TbItemCat> itemList = itemService.getItemInfo(id);

            return Result.result(ConstantEnum.SUCCESS).setData(itemList);
        }catch (Exception e) {

            e.printStackTrace();

            return Result.result(ConstantEnum.ERROR);
        }
    }

    @RequestMapping("addItem")
    @ResponseBody
    public Result addItem(TbItemCat tc) {

        try {

            itemService.addItem(tc);

            return Result.result(ConstantEnum.SUCCESS);

        }catch (Exception e) {

            e.printStackTrace();

            return Result.result(ConstantEnum.ERROR);
        }
    }

    @RequestMapping("getOneItemInfo")
    @ResponseBody
    public Result getOneItemInfo(Long id) {

        try {

            TbItemCat tc = itemService.getOneItemInfo(id);

            return Result.result(ConstantEnum.SUCCESS).setData(tc);
        }catch (Exception e) {

            e.printStackTrace();

            return Result.result(ConstantEnum.ERROR);
        }
    }

    @RequestMapping("updateItem")
    @ResponseBody
    public Result updateItem(TbItemCat tc) {

        try {

            System.out.println(tc);

            itemService.updateItem(tc);

            return Result.result(ConstantEnum.SUCCESS);
        }catch (Exception e) {

            e.printStackTrace();

            return Result.result(ConstantEnum.ERROR);
        }

    }

    @RequestMapping("deleteItem")
    @ResponseBody
    public Result deleteItem(Long[] ids) {

        try {

            String s = itemService.deleteItem(ids);

            return Result.result(ConstantEnum.SUCCESS).setData(s);

        }catch (Exception e) {

            e.printStackTrace();

            return Result.result(ConstantEnum.ERROR);
        }
    }
}
